package com.weikuang.fareast.network;

import com.google.gson.JsonParseException;

import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import io.reactivex.Observer;

public abstract class UserUnBindObserve<T> implements Observer<T> {

    public static final String INTERNAL_SERVER_USER_UNBIND_CODE = "4090231";

    public abstract void onNetError(Throwable e);


    @Override
    public void onComplete() {

    }

    @Override
    public void onError(Throwable e) {
//        if(e != null && e.getMessage() != null && e.getMessage().contains(INTERNAL_SERVER_USER_UNBIND_CODE)){
//            EventBus.getDefault().post(new BaseEvent(Constants.USER_UNBIND));
//        }
        NetErrorException error = null;
        if (e != null) {
            // 对不是自定义抛出的错误进行解析
            if (!(e instanceof NetErrorException)) {
                if (e instanceof UnknownHostException) {
                    error = new NetErrorException(e, NetErrorException.NoConnectError);
                } else if (e instanceof JSONException || e instanceof JsonParseException) {
                    error = new NetErrorException(e, NetErrorException.PARSE_ERROR);
                } else if (e instanceof SocketTimeoutException) {
                    error = new NetErrorException(e, NetErrorException.SocketTimeoutError);
                } else if (e instanceof ConnectException) {
                    error = new NetErrorException(e, NetErrorException.ConnectExceptionError);
                } else {
                    error = new NetErrorException(e, NetErrorException.OTHER);
                }
            } else {
                error = new NetErrorException(e.getMessage(), NetErrorException.OTHER);
            }
        }

        // 回调抽象方法
        onNetError(error);
    }


}